﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    

    public partial class mainForm : Form
    {
        Bitmap bm = null;
        Bitmap bm2 = null;
        Bitmap bm3 = null;
        bool saveFlag = true;
        List<string> historyLog = new List<string>();
        string lastEvent=null;


        public mainForm()
        {
            
            InitializeComponent();
            loadPlugins();
        }

        private void otwórzToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    using (StreamReader sr = new StreamReader(openFileDialog1.FileName + ".log"))
                    {
                        historyLog.Clear();
                        while (sr.Peek() >= 0)
                        {
                            historyLog.Add(sr.ReadLine());
                            if (historyLog.Last() != null && historyLog.Last() != " " && historyLog.Last()!="")
                            textboxHistory.Text += historyLog.Last()+Environment.NewLine;
                        }
                    }
                }
                catch
                {
                    //no log file
                }
                bm = new Bitmap(openFileDialog1.FileName);
            }
            mainPicture.SizeMode = PictureBoxSizeMode.StretchImage;
            thirdPicturbox.SizeMode = PictureBoxSizeMode.StretchImage;
            mainPicture.Image = (Image)bm;
            extendBoth();
        }
        private void loadPlugins()
        {

        }
        private void resizeMe()
        {
            //this.Size= new Size(800,400);
            mainPicture.Width = Size.Width / 3 - 60;
            mainPicture.Height = Size.Height -60;
            mainPicture.Left = 10;
            mainPicture.Top = 30;

            secondPicture.Width = mainPicture.Width;
            secondPicture.Height = mainPicture.Height;
            secondPicture.Left = 2* mainPicture.Width + 30;
            secondPicture.Top = 30;

            thirdPicturbox.Width = mainPicture.Width;
            thirdPicturbox.Height = mainPicture.Height;
            thirdPicturbox.Left =  mainPicture.Width + 20;
            thirdPicturbox.Top = 30;
        }
        private void otwórzToolStripMenuItem1_Click(object sender, EventArgs e)
        {
                          
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                resizeMe();
                button1.Visible = true;
                bm2 = new Bitmap(openFileDialog1.FileName);
                secondPicture.SizeMode = PictureBoxSizeMode.StretchImage;
                secondPicture.Image = (Image)bm2;
                extendBoth();
            }
            
        }
        private void extendBoth()
        {
            
            if (bm2 != null && bm != null)
            {
                Bitmap bm_tmp1 = new Bitmap(bm, Math.Max(bm.Width, bm2.Width), Math.Max(bm.Height, bm2.Height));
                Bitmap bm_tmp2 = new Bitmap(bm2, Math.Max(bm.Width, bm2.Width), Math.Max(bm.Height, bm2.Height));
                bm = bm_tmp1;
                bm2 = bm_tmp2;
            }
        }
        private void zmianaWartościRGBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.addRGB.addColor(bm);
            thirdPicturbox.Image=(Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }
        void save()
        {

            
            saveFileDialog1.Filter = "Bmp files (*.bmp)|*.bmp|All files (*.*)|*.*";
            System.Drawing.Imaging.ImageFormat tmpExt;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                StreamWriter writer = new StreamWriter(saveFileDialog1.FileName+".log");
                foreach(string eventLog in historyLog)
                {
                    writer.WriteLine(eventLog+"\n");

                }
                writer.Close();
                if (saveFileDialog1.FilterIndex == 0)
                {
                    tmpExt = System.Drawing.Imaging.ImageFormat.Bmp;

                }
                if (saveFileDialog1.FilterIndex == 1)
                {
                    tmpExt = System.Drawing.Imaging.ImageFormat.Jpeg;
                }
                if (saveFileDialog1.FilterIndex == 2)
                {
                    tmpExt = System.Drawing.Imaging.ImageFormat.Gif;
                }
                else
                {
                    tmpExt = System.Drawing.Imaging.ImageFormat.Tiff;
                }
                bm3.Save(saveFileDialog1.FileName, tmpExt);

            }
        }

        private void zapiszToolStripMenuItem_Click(object sender, EventArgs e)
        {

            save();
                      
        }

        private void wyjścieToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            Close();
        }

        private void dodajObrazyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.addPictures.addPics(bm, bm2);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void negatywToolStripMenuItem_Click(object sender, EventArgs e)
        {
            myRGB getpxbm= new myRGB();
            bm3 = bm;
            for (int i = 0; i < bm.Width; i++)
            {
                for (int j = 0; j < bm.Height; j++)
                {
                    getpxbm.R = 255-bm.GetPixel(i, j).R;
                    getpxbm.G = 255-bm.GetPixel(i, j).G;
                    getpxbm.B = 255-bm.GetPixel(i, j).B;
                    bm3.SetPixel(i, j,Color.FromArgb(getpxbm.R, getpxbm.G, getpxbm.B));
                }
            }
            thirdPicturbox.Image=(Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void plikToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            save();
        }

        private void horyzontalnieToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3=wtyczki.Flip.horizontal(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void poziomoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Flip.vertical(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Rotate._90(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Rotate._270(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Rotate._180(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void pionowoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Mirror.vertical(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void poziomoToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Mirror.horizontal(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void wytnijToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Cut.doCut(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void erozjaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Morfologic.erozja(bm, 0);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void odcienieSzarościToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.GrayScale.toGray(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void contrastToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Contrast.ContrastThis(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void binarysationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Binarysation.BinaryThis(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void kwaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Kwant.KwantThis(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void wynikowyNaŹrodłoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            historyLog.Add(lastEvent);
            textboxHistory.Text +=  lastEvent+Environment.NewLine;
            if (bm3 != null)
            {
                bm = bm3;
                mainPicture.Image = (Image)bm3;
                mainPicture.Refresh();
            }
        }

        private void thirdPicturbox_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            bm3 = null;
            button1.Visible = false;
            mainPicture.Width = Size.Width / 2 - 80;
            mainPicture.Height = Size.Height - 60;
            mainPicture.Left = 10;
            mainPicture.Top = 30;

            thirdPicturbox.Width = mainPicture.Width;
            thirdPicturbox.Height = mainPicture.Height;
            thirdPicturbox.Left = mainPicture.Width + 20;
            thirdPicturbox.Top = 30;

        }

        private void edycjaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFlag = false;
        }

        private void type1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Transformation.noisereductionType1(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void type2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Transformation.noisereductionType2(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void type3ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Transformation.noisereductionType3(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void type4ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Transformation.noisereductionType4(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void laplasjan1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Transformation.edgedetectionLaplasjan1(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void laplasjan2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Transformation.edgedetectionLaplasjan2(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void laplasjan3ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Transformation.edgedetectionLaplasjan3(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void laplasjan4ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Transformation.edgedetectionLaplasjan4(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void laplasjan5ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Transformation.edgedetectionLaplasjan5(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void laplasjan6ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Transformation.edgedetectionLaplasjan6(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void leftUpperToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Transformation.cornerdetectionUpperLeft(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void rightUpperToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Transformation.cornerdetectionUpperRight(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void leftDownToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Transformation.cornerdetectionDownLeft(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void rightDownToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Transformation.cornerdetectionDownRight(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void roberts45ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Transformation.gradientRoberts45(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void roberts135ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Transformation.gradientRoberts135(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void horizontalPrewittToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Transformation.prewittHorizontal(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void verticalPrewittToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Transformation.prewittVertical(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }

        private void mainForm_Resize(object sender, EventArgs e)
        {
            resizeMe();
        }

        private void mainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            save();
        }

        private void customMaskToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bm3 = wtyczki.Transformation.customMask(bm);
            thirdPicturbox.Image = (Image)bm3;
            thirdPicturbox.Refresh(); ToolStripMenuItem nameX = (ToolStripMenuItem)sender; lastEvent = nameX.Text;
        }
        
        
    }
}
